﻿package com.ease.flex.remoting
{
	import com.ease.util.RemoteService;
	import com.ease.util.ServiceUtil;
	import com.ease.util.events.MethodReturnEvent;
	import com.ease.framework.CallbackUtil;
	import com.obas.flex.common.event.SessionTimeoutEvent;
	import com.obas.flex.common.util.ClientErrorUtil;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;

	import mx.managers.BrowserManager;
	import com.ease.access.auditlog.service.AuditLogAccessService;
	import com.ease.access.workbench.service.CoolActionAccessService;
	import com.ease.access.workbench.service.CoolBarAccessService;
	import com.ease.access.workbench.service.CoolButtonAccessService;
	import com.ease.access.workbench.service.CoolMenuAccessService;
	import com.ease.access.workbench.service.CoolPerspectiveAccessService;
	import com.ease.access.workbench.service.CoolPluginAccessService;
	import com.ease.access.workbench.service.CoolViewAccessService;
	import com.ease.platform.workbench.service.WorkbenchManagementService;

	public class ServiceManagerComEaseBusinessFlexRemoting {

		private static var _instance:ServiceManagerComEaseBusinessFlexRemoting = null;

		public function ServiceManagerComEaseBusinessFlexRemoting(singleton:SingletonClass) {
			var contextPath:String = ExternalInterface.call("BrowserHistory.getURL");
			RemoteService.defaultChannelUri = ServiceUtil.getDefaultChannelUri(contextPath);
		}

		public static function onOperationReturn(eventDispatcher:EventDispatcher, event:MethodReturnEvent):void {
	    	//var response:Event = null;
			//response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
			//eventDispatcher.dispatchEvent(response);
			
			var response:Event = null;
         	//Session timeout
         	if( null != event.error && ClientErrorUtil.getErrorType(event.error) == ClientErrorUtil.AUTHENTICATION_ERROR) {
            	var sessionTimeoutEvent:SessionTimeoutEvent = new SessionTimeoutEvent(SessionTimeoutEvent.SESSION_TIMEOUT);
            	eventDispatcher.dispatchEvent(sessionTimeoutEvent);
            	response = CallbackUtil.createResponseFromRequest(event.callContext, null, null);
         	} else {
            	response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
         	}
         	eventDispatcher.dispatchEvent(response);
		}

	    public static function getInstance():ServiceManagerComEaseBusinessFlexRemoting {
	        if (_instance == null) {
				_instance = new ServiceManagerComEaseBusinessFlexRemoting(new SingletonClass());
	        }

	        return _instance;
	    }
		private var _auditLogAccessService:AuditLogAccessService = null;

		public static function getAuditLogAccessService():AuditLogAccessService {
			if (getInstance()._auditLogAccessService == null) {
				getInstance()._auditLogAccessService = new AuditLogAccessService();
			}
			return getInstance()._auditLogAccessService;
		}

		public function set enabledAuditLogAccessService(value:Boolean):void {
			if (getInstance()._auditLogAccessService) {
				getInstance()._auditLogAccessService.enabled = value;
			}
		}
		private var _coolActionAccessService:CoolActionAccessService = null;

		public static function getCoolActionAccessService():CoolActionAccessService {
			if (getInstance()._coolActionAccessService == null) {
				getInstance()._coolActionAccessService = new CoolActionAccessService();
			}
			return getInstance()._coolActionAccessService;
		}

		public function set enabledCoolActionAccessService(value:Boolean):void {
			if (getInstance()._coolActionAccessService) {
				getInstance()._coolActionAccessService.enabled = value;
			}
		}
		private var _coolBarAccessService:CoolBarAccessService = null;

		public static function getCoolBarAccessService():CoolBarAccessService {
			if (getInstance()._coolBarAccessService == null) {
				getInstance()._coolBarAccessService = new CoolBarAccessService();
			}
			return getInstance()._coolBarAccessService;
		}

		public function set enabledCoolBarAccessService(value:Boolean):void {
			if (getInstance()._coolBarAccessService) {
				getInstance()._coolBarAccessService.enabled = value;
			}
		}
		private var _coolButtonAccessService:CoolButtonAccessService = null;

		public static function getCoolButtonAccessService():CoolButtonAccessService {
			if (getInstance()._coolButtonAccessService == null) {
				getInstance()._coolButtonAccessService = new CoolButtonAccessService();
			}
			return getInstance()._coolButtonAccessService;
		}

		public function set enabledCoolButtonAccessService(value:Boolean):void {
			if (getInstance()._coolButtonAccessService) {
				getInstance()._coolButtonAccessService.enabled = value;
			}
		}
		private var _coolMenuAccessService:CoolMenuAccessService = null;

		public static function getCoolMenuAccessService():CoolMenuAccessService {
			if (getInstance()._coolMenuAccessService == null) {
				getInstance()._coolMenuAccessService = new CoolMenuAccessService();
			}
			return getInstance()._coolMenuAccessService;
		}

		public function set enabledCoolMenuAccessService(value:Boolean):void {
			if (getInstance()._coolMenuAccessService) {
				getInstance()._coolMenuAccessService.enabled = value;
			}
		}
		private var _coolPerspectiveAccessService:CoolPerspectiveAccessService = null;

		public static function getCoolPerspectiveAccessService():CoolPerspectiveAccessService {
			if (getInstance()._coolPerspectiveAccessService == null) {
				getInstance()._coolPerspectiveAccessService = new CoolPerspectiveAccessService();
			}
			return getInstance()._coolPerspectiveAccessService;
		}

		public function set enabledCoolPerspectiveAccessService(value:Boolean):void {
			if (getInstance()._coolPerspectiveAccessService) {
				getInstance()._coolPerspectiveAccessService.enabled = value;
			}
		}
		private var _coolPluginAccessService:CoolPluginAccessService = null;

		public static function getCoolPluginAccessService():CoolPluginAccessService {
			if (getInstance()._coolPluginAccessService == null) {
				getInstance()._coolPluginAccessService = new CoolPluginAccessService();
			}
			return getInstance()._coolPluginAccessService;
		}

		public function set enabledCoolPluginAccessService(value:Boolean):void {
			if (getInstance()._coolPluginAccessService) {
				getInstance()._coolPluginAccessService.enabled = value;
			}
		}
		private var _coolViewAccessService:CoolViewAccessService = null;

		public static function getCoolViewAccessService():CoolViewAccessService {
			if (getInstance()._coolViewAccessService == null) {
				getInstance()._coolViewAccessService = new CoolViewAccessService();
			}
			return getInstance()._coolViewAccessService;
		}

		public function set enabledCoolViewAccessService(value:Boolean):void {
			if (getInstance()._coolViewAccessService) {
				getInstance()._coolViewAccessService.enabled = value;
			}
		}
		private var _workbenchManagementService:WorkbenchManagementService = null;

		public static function getWorkbenchManagementService():WorkbenchManagementService {
			if (getInstance()._workbenchManagementService == null) {
				getInstance()._workbenchManagementService = new WorkbenchManagementService();
			}
			return getInstance()._workbenchManagementService;
		}

		public function set enabledWorkbenchManagementService(value:Boolean):void {
			if (getInstance()._workbenchManagementService) {
				getInstance()._workbenchManagementService.enabled = value;
			}
		}

	}
}
class SingletonClass{}
